<template>

	<div>
		<el-dialog :title="title" :visible.sync="dialogVisible" width="900px" :before-close="handleClose"
			:close-on-click-modal="false">
			<div>
				<el-form :model="queryParams" ref="zone-maintenance-queryForm" size="small" :inline="true"
					label-width="120px" v-if="state == 2">
					<div class="app-space-between">
						<div class="app-space-between-left">
							<el-form-item label="商品名称:" prop="goodsName">
								<el-input v-model="queryParams.goodsName" class="cell-input" placeholder="请输入商品名称"
									clearable @clear="handleQuery" @keyup.enter.native="handleQuery" />
							</el-form-item>
							<el-form-item label="商品类目:" prop="goodsCategoryId">
								<div class="cell-input">
									<treeselect v-model="queryParams.goodsCategoryId" :options="testOptions"
										:normalizer="normalizer" :show-count="true" placeholder="主类目" />
								</div>
							</el-form-item>
							<el-form-item label="商品分类:" prop="goodsTypeId">
								<div class="cell-input">
									<treeselect v-model="queryParams.goodsTypeId" :options="testOptionsType"
										:normalizer="normalizerType" :show-count="true" placeholder="主类目" />
								</div>
							</el-form-item>
						</div>
						<div class="app-space-between-right">
							<el-form-item>
								<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
								<el-button size="mini" @click="resetQuery">重置</el-button>
							</el-form-item>
						</div>
					</div>
				</el-form>

				<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange"
					ref="shopping-add-table">
					<el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
					<el-table-column show-overflow-tooltip label="专题名称" width="140" align="center" prop="goodsName" />
					<el-table-column show-overflow-tooltip label="供应商" align="center" prop="supplier" />
					
					<!-- <el-table-column show-overflow-tooltip label="配置方式" align="center">
						<template slot-scope="scope">
							{{scope.row.collocationMethod == '0' ? '手动配置' : '自动配置' }}
						</template>
					</el-table-column> -->
					
					<el-table-column show-overflow-tooltip label="创建时间" align="center" prop="creatorTime">
						<template slot-scope="scope">
							{{scope.row.creatorTime | $YYYY_MM_DD_hh_mm_ss}}
						</template>
					</el-table-column>
					<el-table-column show-overflow-tooltip label="操作" align="center" width="140" fixed="right"
						v-if="state == 1">
						<template slot-scope="scope">
							<el-link type="primary" @click="handlewhDel(scope.row)" :underline="false">删除</el-link>
						</template>
					</el-table-column>
				</el-table>
				<!-- <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="getList" /> -->
			</div>


			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">{{state == 2?'取 消' : '关 闭'}}</el-button>
				<el-button type="primary" @click="handleSubmit" :loading="btnLoading" v-if="state == 2"
					:disabled="loading">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import {
		findWcGetGoodsList,
		wcGoodsContactZoneAdds,
		deleteWcGoodsContactZone
	} from "@/api/culturalCreativity/zone.js";
	import {
		listWcGoodsCategory
	} from "@/api/culturalCreativity/category.js";

	import {
		listWcGoods
	} from "@/api/culturalCreativity/shopping.js";
	import {
		listWcGoodsType
	} from "@/api/culturalCreativity/classification.js";
	import Treeselect from "@riophae/vue-treeselect";
	import "@riophae/vue-treeselect/dist/vue-treeselect.css";
	let _this;
	export default {
		components: {
			Treeselect
		},
		data() {
			return {
				ids: [],
				formInline: {},
				title: "",
				total: 0,
				queryParams: {
					goodsName: "",
					goodsCategoryId: "",
					productIntroduction: 1,
					goodsTypeId: "",
				},
				postList: [],
				dialogVisible: false,
				loading: false,
				btnLoading: false,
				state: 0,
				menuOptions: [],
				testOptions: [],
				testOptionsType:[],
				classificationData: [],
				zoneName: '',
				zoneId: ''
			}
		},
		methods: {
			async init(e, state) {
				this.state = state;
				this.dialogVisible = true;
				this.zoneName = e.zoneName;
				this.zoneId = e.id;
				let res;
				if (state == 1) {
					this.getDelList();
				} else {
					this.loading = true;
					await this.getLeiMu();
					await this.getLeiMuType();
					let res1 = await listWcGoodsType({}, {
						pageNum: 1,
						pageSize: 9999,
					});
					this.classificationData = res1.rows;
					this.getList();
				}
			},
			async getDelList() {
				this.loading = true;
				let res = await findWcGetGoodsList();
				this.postList = res.data[this.zoneName];
				this.loading = false;
			},
			async handlewhDel(row) {
				await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				});
				await deleteWcGoodsContactZone({
					ids: row.id,
					zoneId: this.zoneId
				});
				this.$message.success('删除成功!');
				this.$emit("success");
				this.getDelList();
			},
			async handleSubmit() {
				await Promise.all(this.menuOptions.map(goodsId => {
					return new Promise(async (resolve, reject) => {
						await wcGoodsContactZoneAdds({
							zoneId: this.zoneId,
							zoneName: this.zoneName,
							goodsId
						});
						resolve();
					})
				}));
				this.$emit("success");
				this.loading = false;
				this.dialogVisible = false;
			},
			async getList() {
				try {
					this.loading = true;
					this.menuOptions = [];
					let res1 = await findWcGetGoodsList();
					let res2 = await listWcGoods({}, {
						...this.queryParams,
						pageNum: 1,
						pageSize: 99999
					});
					this.postList = res2.rows.reduce((pev, item) => {
						if (!res1.data[this.zoneName].some(i => i.goodsId == item.goodsId)) {
							pev.push(item);
						}
						return pev
					}, []);
					console.log(res1.data[this.zoneName]);
					console.log(res2.rows);
					this.loading = false;
				} catch (e) {
					console.error(e);
					this.loading = false;
				}
			},
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.menuOptions = selection.map(item => item.goodsId);
			},
			/** 转换菜单数据结构 */
			normalizerType(node) {
				if (node.children && !node.children.length) {
					delete node.children;
				};
				return {
					id: node.goodsTypeId,
					label: node.goodsTypeName,
					children: node.children
				};
			},
			resetFrom() {
				this.formInline = {
					name: "",
					localSite: "",
					duties: "",
					phone: "",
					attachmentAddress: "",
					beginTime: "",
					endTime: "",
					list: [],
					publicLeaveDays: "",
					reason: "",
					arrive: ""
				}
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("zone-maintenance-queryForm");
				this.handleQuery();
			},
			handleClose() {
				this.dialogVisible = false;
			},
			async getLeiMu() {
				this.testOptions = [];
				let res = await listWcGoodsCategory({}, {
					pageNum: 1,
					pageSize: 99999,
				});
				const menu = {
					goodsCategoryId: '',
					goodsCategoryName: '主类目',
					children: []
				};
				menu.children = this.handleTree(res.rows, "goodsCategoryId", "goodsCategoryParentId");
				this.testOptions.push(menu);
				return;
			},
			async getLeiMuType() {
				this.testOptionsType = [];
				let res = await listWcGoodsType({}, {
					pageNum: 1,
					pageSize: 99999,
				});
				const menu = {
					goodsTypeId: '',
					goodsTypeName: '主类目',
					children: []
				};
				menu.children = this.handleTree(res.rows, "goodsTypeId","modifyOperatorId");
				this.testOptionsType.push(menu);
				return;
			},
			/** 转换菜单数据结构 */
			normalizer(node) {
				if (node.children && !node.children.length) {
					delete node.children;
				}
				return {
					id: node.goodsCategoryId,
					label: node.goodsCategoryName,
					children: node.children
				};
			}
		},
		created() {
			this.resetFrom();
		},
		computed: {}
	}
</script>
<style lang="scss" scoped>
	.width100 {
		width: 590px;
	}

	.cell-input {
		width: 200px;
	}

	.up-file-wrappar {
		display: flex;
		align-items: center;
	}

	.total-day {
		display: inline-block;
		height: 36px;
		width: 40px;
		margin: 0 6px;
		text-align: center;
		border-radius: 4px;
		border: 1px solid #DCDFE6;
		padding: 0 15px;
		box-sizing: border-box;
	}

	.up-file-name {
		display: inline-block;
		height: 36px;
		min-width: 555px;
		text-align: left;
		line-height: 36px;
		box-sizing: border-box;
		padding: 0 15px;
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		color: #606266;
		font-size: inherit;

		.text {
			display: -webkit-box;
			overflow: hidden;
			white-space: normal !important;
			text-overflow: ellipsis;
			word-wrap: break-word;
			-webkit-line-clamp: 1; //行数
			-webkit-box-orient: vertical;
			max-width: 555px;
		}
	}
</style>
